package com.itlaobing.util;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

public class PageTag extends TagSupport{

	//当前页
	private String pageIndex;
	//每页的大小
	private String pageSize;
	//总条数
	private String recordCount;
	//总页数
	private String pageCount;
	//页码数组
	private int[] pageNums=new int[0];
	//请求的地址
	private String actionName;
	//前面是否有省略号
	private boolean hasPreOmit;
	//后面是否有省略号
	private boolean hasSuffixOmit;

	//条件
	private String condition;







	public String getCondition() {
		return condition;
	}





	public void setCondition(String condition) {
		this.condition = condition;
	}





	public String getPageIndex() {
		return pageIndex;
	}





	public void setPageIndex(String pageIndex) {
		this.pageIndex = pageIndex;
	}





	public String getPageSize() {
		return pageSize;
	}





	public void setPageSize(String pageSize) {
		this.pageSize = pageSize;
	}





	public String getRecordCount() {
		return recordCount;
	}





	public void setRecordCount(String recordCount) {
		this.recordCount = recordCount;
	}





	public String getPageCount() {
		return pageCount;
	}





	public void setPageCount(String pageCount) {
		this.pageCount = pageCount;
	}





	public int[] getPageNums() {
		return pageNums;
	}





	public void setPageNums(int[] pageNums) {
		this.pageNums = pageNums;
	}





	public String getActionName() {
		return actionName;
	}





	public void setActionName(String actionName) {
		this.actionName = actionName;
	}





	public boolean isHasPreOmit() {
		return hasPreOmit;
	}





	public void setHasPreOmit(boolean hasPreOmit) {
		this.hasPreOmit = hasPreOmit;
	}





	public boolean isHasSuffixOmit() {
		return hasSuffixOmit;
	}





	public void setHasSuffixOmit(boolean hasSuffixOmit) {
		this.hasSuffixOmit = hasSuffixOmit;
	}





	@Override
	public int doStartTag() throws JspException {

		int size =Integer.parseInt(pageSize);
		int index = Integer.parseInt(pageIndex);
		int record = Integer.parseInt(recordCount);
		int pageCount=(record-1)/size+1;
		try {
			//获取输出对象
			JspWriter out = pageContext.getOut();

			out.print("<ul class=\"pagination\">");
			out.print("<li><a href=\""+actionName+"?pageIndex=1&condition="+condition+"\">&laquo;</a></li>");
			//判断之前是否显示省略号
			if(hasPreOmit) {
				out.print("<li><a>...</a></li>");
			}

			for(int i=0;i<pageNums.length;i++) {

				if(index==pageNums[i]) {
					out.print("<li class=\"active\"><a href=\""+actionName+"?pageIndex="+pageNums[i]+"&condition="+condition+"\">"+pageNums[i]+"</a></li>");
				}else {
					out.print("<li><a href=\""+actionName+"?pageIndex="+pageNums[i]+"&condition="+condition+"\">"+pageNums[i]+"</a></li>");
				}
			}


			//是否之后显示省略号
			if(hasSuffixOmit) {
				out.print("<li><a>...</a></li>");
			}

			out.print("<li><a href=\""+actionName+"?pageIndex="+pageCount+"&condition="+condition+"\">&raquo;</a></li>");
			out.print("</ul>");





		} catch (IOException e) {
			e.printStackTrace();
		}
		return SKIP_BODY;
	}

}
